<template>
	<el-main class="bg edit_wrap">
		<el-form ref="form" :model="form" status-icon label-width="120px" v-if="is_view()">

							<el-col v-if="user_group === '管理员' || $check_field('get','meeting_number') || $check_field('add','meeting_number') || $check_field('set','meeting_number')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="会议编号" prop="meeting_number">
												<el-input id="meeting_number" v-model="form['meeting_number']" placeholder="请输入会议编号"
							  v-if="user_group === '管理员' || (form['user_notifications_id'] && $check_field('set','meeting_number')) || (!form['user_notifications_id'] && $check_field('add','meeting_number'))" :disabled="disabledObj['meeting_number_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','meeting_number')">{{form['meeting_number']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="user_group === '管理员' || $check_field('get','conference_name') || $check_field('add','conference_name') || $check_field('set','conference_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="会议名称" prop="conference_name">
												<el-input id="conference_name" :value="getChineseText(form['conference_name'])" placeholder="请输入会议名称"
							  v-if="user_group === '管理员' || (form['user_notifications_id'] && $check_field('set','conference_name')) || (!form['user_notifications_id'] && $check_field('add','conference_name'))" :disabled="true"></el-input>
					<div v-else-if="$check_field('get','conference_name')">{{getChineseText(form['conference_name'])}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="user_group === '管理员' || $check_field('get','meeting_type') || $check_field('add','meeting_type') || $check_field('set','meeting_type')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="会议类型" prop="meeting_type">
												<el-input id="meeting_type" :value="getChineseText(form['meeting_type'])" placeholder="请输入会议类型"
							  v-if="user_group === '管理员' || (form['user_notifications_id'] && $check_field('set','meeting_type')) || (!form['user_notifications_id'] && $check_field('add','meeting_type'))" :disabled="true"></el-input>
					<div v-else-if="$check_field('get','meeting_type')">{{getChineseText(form['meeting_type'])}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="user_group === '管理员' || $check_field('get','meeting_time') || $check_field('add','meeting_time') || $check_field('set','meeting_time')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="会议时间" prop="meeting_time">
												<el-input id="meeting_time" v-model="form['meeting_time']" placeholder="请输入会议时间"
							  v-if="user_group === '管理员' || (form['user_notifications_id'] && $check_field('set','meeting_time')) || (!form['user_notifications_id'] && $check_field('add','meeting_time'))" :disabled="disabledObj['meeting_time_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','meeting_time')">{{form['meeting_time']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="user_group === '管理员' || $check_field('get','meeting_venue') || $check_field('add','meeting_venue') || $check_field('set','meeting_venue')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="会议地点" prop="meeting_venue">
												<el-input id="meeting_venue" :value="getChineseText(form['meeting_venue'])" placeholder="请输入会议地点"
							  v-if="user_group === '管理员' || (form['user_notifications_id'] && $check_field('set','meeting_venue')) || (!form['user_notifications_id'] && $check_field('add','meeting_venue'))" :disabled="true"></el-input>
					<div v-else-if="$check_field('get','meeting_venue')">{{getChineseText(form['meeting_venue'])}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="user_group === '管理员' || $check_field('get','participants') || $check_field('add','participants') || $check_field('set','participants')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="参会用户" prop="participants">
													<el-select v-if="user_group === '管理员' || (form['user_notifications_id'] && $check_field('set','participants')) || (!form['user_notifications_id'] && $check_field('add','participants'))" id="participants" v-model="form['participants']" :disabled="disabledObj['participants_isDisabled']">
							<el-option v-for="o in list_user_participants" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
						<el-select v-else-if="$check_field('get','participants')" id="participants" v-model="form['participants']" :disabled="true">
							<el-option v-for="o in list_user_participants" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
											</el-form-item>
			</el-col>
								<el-col v-if="user_group === '管理员' || $check_field('get','user_name') || $check_field('add','user_name') || $check_field('set','user_name')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="用户姓名" prop="user_name">
												<el-input id="user_name" v-model="form['user_name']" placeholder="请输入用户姓名"
							  v-if="user_group === '管理员' || (form['user_notifications_id'] && $check_field('set','user_name')) || (!form['user_notifications_id'] && $check_field('add','user_name'))" :disabled="disabledObj['user_name_isDisabled']"></el-input>
					<div v-else-if="$check_field('get','user_name')">{{form['user_name']}}</div>
											</el-form-item>
			</el-col>
								<el-col v-if="user_group === '管理员' || $check_field('get','can_i_attend_the_meeting') || $check_field('add','can_i_attend_the_meeting') || $check_field('set','can_i_attend_the_meeting')" :xs="24" :sm="12" :lg="8" class="el_form_item_warp">
				<el-form-item label="能否参会" prop="can_i_attend_the_meeting">
								<el-select id="can_i_attend_the_meeting" v-model="form['can_i_attend_the_meeting']"
						v-if="user_group === '管理员' || (form['user_notifications_id'] && $check_field('set','can_i_attend_the_meeting')) || (!form['user_notifications_id'] && $check_field('add','can_i_attend_the_meeting'))">
						<el-option v-for="o in list_can_i_attend_the_meeting" :key="o" :label="o" :value="o">
						</el-option>
					</el-select>
					<div v-else-if="$check_field('get','can_i_attend_the_meeting')">{{form['can_i_attend_the_meeting']}}</div>
							</el-form-item>
			</el-col>
								<el-col v-if="user_group === '管理员' || $check_field('get','notification_content') || $check_field('add','notification_content') || $check_field('set','notification_content')" :xs="24" :sm="24" :lg="24" class="el_form_item_warp">
				<el-form-item label="通知内容" prop="notification_content">
					<BilingualInput 
						v-if="user_group === '管理员' || (form['user_notifications_id'] && $check_field('set','notification_content')) || (!form['user_notifications_id'] && $check_field('add','notification_content'))"
						v-model="form['notification_content']"
						field="notification_content"
						label="通知内容"
						input-type="textarea"
						:input-props="{ rows: 4 }"
						:disabled="disabledObj['notification_content_isDisabled']"
					/>
					<div v-else-if="$check_field('get','notification_content')">{{$getBilingualText(form['notification_content'])}}</div>
				</el-form-item>
			</el-col>
					
	
	
	
	
	
	
			<el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
				<el-form-item>
					<el-button type="primary" @click="submit()">提交</el-button>
					<el-button @click="cancel()">取消</el-button>
					<el-button type="success" @click="sendSms()" :disabled="!form['user_notifications_id'] || form['user_notifications_id'] == 0" icon="el-icon-message">发送短信</el-button>
				</el-form-item>
			</el-col>

		</el-form>
	</el-main>
</template>

<script>
	import mixin from "@/mixins/page.js";
	import BilingualInput from "@/components/common/BilingualInput.vue";
	import mixinI18n from "@/mixins/i18n.js";

	export default {
		mixins: [mixin, mixinI18n],
		components: {
			BilingualInput
		},
		data() {
			return {
				field: "user_notifications_id",
				url_add: "~/api/user_notifications/add?",
				url_set: "~/api/user_notifications/set?",
				url_get_obj: "~/api/user_notifications/get_obj?",
				url_upload: "~/api/user_notifications/upload?",

				query: {
					"user_notifications_id": 0,
				},

				form: {
								"meeting_number":  '', // 会议编号
										"conference_name":  '', // 会议名称
										"meeting_type":  '', // 会议类型
										"meeting_time":  '', // 会议时间
										"meeting_venue":  '', // 会议地点
										"participants": 0, // 参会用户
										"user_name":  '', // 用户姓名
										"can_i_attend_the_meeting":  '', // 能否参会
										"notification_content":  '', // 通知内容
											"user_notifications_id": 0, // ID
						
				},
				disabledObj:{
								"meeting_number_isDisabled": false,
										"conference_name_isDisabled": false,
										"meeting_type_isDisabled": false,
										"meeting_time_isDisabled": false,
										"meeting_venue_isDisabled": false,
										"participants_isDisabled": false,
										"user_name_isDisabled": false,
										"can_i_attend_the_meeting_isDisabled": false,
										"notification_content_isDisabled": false,
										},

	
		
		
		
		
		
					// 用户列表
				list_user_participants: [],
				
						// 能否参会选项列表
				list_can_i_attend_the_meeting: ['是','否'],
	
		
	
			}
		},
		methods: {


	
	
			
	
			
	
			
	
			
	
			
	
				/**
			 * 获取普通用户用户列表
			 */
			async get_list_user_participants() {
                // if(this.user_group !== "管理员" && this.form["participants"] === 0) {
                //     this.form["participants"] = this.user.user_id;
                // }
                var json = await this.$get("~/api/user/get_list?user_group=普通用户");
                if(json.result && json.result.list){
                    this.list_user_participants = json.result.list;
                }
                else if(json.error){
                    console.error(json.error);
                }
			},
					get_user_participants(id){
				var obj = this.list_user_participants.getObj({"user_id":id});
				var ret = "";
				if(obj){
					if(obj.nickname){
						ret = obj.nickname;}
					else{
						ret = obj.username;
					}
				}
				return ret;
			},
			
	
			
	
			
	
		
			/**
			 * 获取对象之前
			 * @param {Object} param
			 */
			get_obj_before(param) {
				var form = "";
																																												// 获取缓存数据附加
				form = $.db.get("form");
							$.push(this.form ,form);
																
				if(this.form && form){
					Object.keys(this.form).forEach(key => {
						Object.keys(form).forEach(dbKey => {
							// if(dbKey === "charging_standard"){
							// 	this.form['charging_rules'] = form[dbKey];
							// 	this.disabledObj['charging_rules_isDisabled'] = true;
							// };
							if(key === dbKey){
								this.disabledObj[key+'_isDisabled'] = true;
							}
						})
					})
				}
																						$.db.del("form");
				return param;
			},

			/**
			 * 获取对象之后
			 * @param {Object} json
			 * @param {Object} func
			 */
			get_obj_after(json, func){


																											

			},

			/**
			 * 提交前验证事件
			 * @param {Object} 请求参数
			 * @return {String} 验证成功返回null, 失败返回错误提示
			 */
			submit_check(param) {
				let msg = null
																																	return msg;
			},

			is_view(){
				var bl = this.user_group == "管理员";

				if(!bl){
					bl = this.$check_action('/user_notifications/table','add');
					console.log(bl ? "你有表格添加权限视作有添加权限" : "你没有表格添加权限");
				}
				if(!bl){
					bl = this.$check_action('/user_notifications/table','set');
					console.log(bl ? "你有表格添加权限视作有修改权限" : "你没有表格修改权限");
				}
				if(!bl){
					bl = this.$check_action('/user_notifications/view','add');
					console.log(bl ? "你有视图添加权限视作有添加权限" : "你没有视图添加权限");
				}
				if(!bl){
					bl = this.$check_action('/user_notifications/view','set');
					console.log(bl ? "你有视图修改权限视作有修改权限" : "你没有视图修改权限");
				}
				if(!bl){
					bl = this.$check_action('/user_notifications/view','get');
					console.log(bl ? "你有视图查询权限视作有查询权限" : "你没有视图查询权限");
				}

				console.log(bl ? "具有当前页面的查看权，请注意这不代表你有字段的查看权" : "无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行");

				return bl;
			},
			/**
			 * 上传文件
			 * @param {Object} param
			 */
			uploadimg(param) {
				this.uploadFile(param.file, "avatar");
			},
			/**
			 * 发送短信通知
			 */
			async sendSms() {
				if (!this.form['user_notifications_id'] || this.form['user_notifications_id'] == 0) {
					this.$message.warning('请先保存通知信息');
					return;
				}

				this.$message.info('正在发送短信...');
				try {
					const json = await this.$post("~/api/user_notifications/send_sms", {
						user_notifications_id: this.form['user_notifications_id']
					});

					if (json.result && json.result.success) {
						const content = json.result.content || '';
						this.$message.success('短信发送成功！');
						if (json.result.mock) {
							this.$alert('短信内容（模拟模式）：' + content, '短信发送成功', {
								type: 'success',
								confirmButtonText: '确定'
							});
						}
					} else {
						const errorMsg = json.result?.message || json.error?.message || '发送失败';
						this.$message.error('短信发送失败：' + errorMsg);
					}
				} catch (error) {
					console.error('发送短信失败:', error);
					this.$message.error('发送短信失败：' + (error.message || '请稍后重试'));
				}
			},
			// 获取中文文本（从双语JSON中提取）
			getChineseText(str) {
				if (!str) return '';
				try {
					const json = JSON.parse(str);
					if (json && typeof json === 'object' && json.zh) {
						return json.zh;
					}
				} catch (e) {
					// 不是 JSON 格式，直接返回原值（默认为中文）
				}
				return str;
			}

		},
		created() {
															this.get_list_user_participants();
											},
	}
</script>

<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}




</style>
